package org.graduation.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.graduation.model.common.ResponseResult;
import org.graduation.model.common.user.User;
import org.graduation.model.dtos.RegisterDTO;
import org.graduation.model.dtos.user.UpdatePasswordDTO;
import org.graduation.user.service.UserService;
import org.graduation.utils.Encrypt;
import org.graduation.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private Token tokenTool;
    @Autowired
    private Encrypt encrypt;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseResult login(@RequestBody User user) {
        return userService.login(user);
    }

    @PostMapping("/register")
    public ResponseResult register(@RequestBody RegisterDTO registerDTO) {
        return userService.register(registerDTO);
    }

    @GetMapping("/info")
    public ResponseResult<User> getInfoByToken(@RequestHeader("Authorization")String tokenStr){
        Integer uid = tokenTool.getUidByToken(tokenStr);
        User user = userService.getById(uid);
        user.hiddenPassword();
        return ResponseResult.success(user);
    }
    @GetMapping("/info/email")
    public ResponseResult info(@RequestParam String email) {
        return userService.getInfoByEmail(email);
    }

    @GetMapping("/info/u_id/{u_id}")
    public ResponseResult<User> info(@PathVariable int u_id) {
        return ResponseResult.success(userService.getById(u_id));
    }

    @PostMapping("/search/list/uid")
    public ResponseResult<List<User>> search(@RequestBody List<Integer> uidList) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .in(User::getUid, uidList)
                .select(User::getUid, User::getNickname, User::getEmail)
        ;
        return ResponseResult.success(userService.list(lambdaQueryWrapper));
    }

    @PostMapping("/info/update/password")
    public ResponseResult updatePassword(@RequestHeader("Authorization") String tokenStr, @RequestBody UpdatePasswordDTO dto) {
        Integer uid = tokenTool.getUidByToken(tokenStr);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(User::getUid, uid)
                .eq(User::getPassword, encrypt.md5_32(dto.getOldPassword()));
        User user = userService.getOne(queryWrapper);
        if(user != null){
            user.setPassword(encrypt.md5_32(dto.getNewPassword()));
            return userService.updateById(user) ? ResponseResult.success() : ResponseResult.failed();
        }else{
            return ResponseResult.failed();
        }
    }
    @PostMapping("/info/update")
    public ResponseResult<User> updateInfo(@RequestBody User user,@RequestHeader("Authorization")String tokenStr){
        if(!user.getUid().equals(tokenTool.getUidByToken(tokenStr))){
            return ResponseResult.failed("信息错误");
        }else{
            LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper
                    .eq(User::getUid,user.getUid())
                    .set(User::getNickname,user.getNickname());
            return userService.update(updateWrapper) ? ResponseResult.success(user) : ResponseResult.failed();
        }
    }
}
